document.write("<script src=\"" + context
		+ "/system/wordbook/js/wbstore.js\"></script>");
document.write("<script src=\"" + context
		+ "/system/purview/js/purviewlist4tab.js\"></script>");
document.write("<script src=\"" + context
		+ "/system/purview/js/purviewobjselect.js\"></script>");
document.write("<script src=\"" + context
		+ "/system/tablequery/js/tablehandler.js\"></script>");
document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/system/purview/css/purviewmanage.css"/>');
document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/system/group/css/groupmanage.css"/>');

var querykey = 'list';
var pageSize = 15;
var showchk = false;
var ds;

var chkColumModel = [];
chkColumModel.push({
			'header' : '&nbsp;',
			'dataIndex' : 'PAGINATION_NUMBER',
			sortable : true,
			clazz : 'PAGINATION_NUMBER'
		});
chkColumModel.push({
			'header' : '设计单元名称',
			'dataIndex' : 'C_NAME',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '建管单位',
			'dataIndex' : 'C_UNIT',
			sortable : true,
			clazz : 'SEX',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '建管模式',
			'dataIndex' : 'C_TYPE',
			sortable : true,
			clazz : 'C_TYPE',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '行政区域',
			'dataIndex' : 'C_AREA',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});
chkColumModel.push({
			'header' : '标段名称',
			'dataIndex' : 'CELLBDLIST',
			sortable : true,
			clazz : '',
			renderer : renderHeader
		});

var cm = new Ext.grid.ColumnModel(chkColumModel);

var fields = ['PAGINATION_NUMBER', 'C_ID', 'USERID', 'USERNAME', 'C_NAME',
		'C_UNIT', 'C_TYPE', 'C_AREA', 'C_TIME', 'CELLBDLIST'];

function initQueryList() {
	var toolbar = new Ext.Toolbar({
		id : querykey + '_toolbar',
		autoWidth : true,
		autoShow : true,
		items : [{
			iconCls : 'query',
			text : '数据查询',
			handler : function() {
				var queryForm = new Ext.FormPanel({
							labelWidth : 75, // label settings here cascade
							// unless
							// overridden
							frame : false,
							bodyStyle : 'padding:5px 5px 0',
							width : 350,
							defaults : {
								width : 230
							},
							defaultType : 'textfield',

							items : [
									{
										fieldLabel : '设计单元',
										id : 'cname',
										name : 'cname'
									},
									{
										fieldLabel : '建管单位',
										id : 'cunit',
										name : 'cunit'
									},
									getWBComboStore('celltype', '', 'ctype_',
											'建管模式', 'ctype', null, '', true), {
										xtype : 'hidden',
										id : 'ctype',
										name : 'ctype'
									}, {
										fieldLabel : '行政区域',
										id : 'carea',
										name : 'carea'
									}]
						});

				var formUrl = context + '/system/result4form.jsp?1=1';

				if ($('cname_')) {
					formUrl += '&cname=' + $('cname_').value;
				}

				if ($('cunit_')) {
					formUrl += '&cunit=' + $('cunit_').value;
				}

				if ($('ctype1')) {
					formUrl += '&ctype=' + $('ctype1').value;
					formUrl += '&ctype_=' + $('ctype2').value;
				}

				if ($('carea_')) {
					formUrl += '&carea=' + $('carea_').value;
				}

				queryForm.getForm().load({
							url : encodeURI(formUrl),
							waitTitle : '提示',
							waitMsg : '正在加载数据,请稍候...',
							animEl : "loding",
							success : function(form, action) {

							},
							failure : function(form, action) {
								Ext.Msg.alert('初始化查询项失败');
							}
						});

				var queryWin = new Ext.Window({
					renderTo : Ext.getBody(),
					layout : 'fit',
					width : 450,
					height : 300,
					title : '数据查询',
					resizable : true,
					plain : true,
					modal : true,

					items : [queryForm],

					buttons : [{
						id : 'objectQueryButton',
						text : '查 询',
						handler : function() {
							createHidden('cname_', queryForm.form
											.findField('cname').getValue());
							createHidden('cunit_', queryForm.form
											.findField('cunit').getValue());
							createHidden('ctype1', queryForm.form
											.findField('ctype').getValue());
							createHidden('ctype2', queryForm.form
											.findField('ctype').getValue());
							createHidden('carea_', queryForm.form
											.findField('carea').getValue()),

							ds.reload({
										params : getQueryTestParams(),
										callback : function(r, options, success) {
											initInfoList(r, options, success);
										}
									});
							queryWin.close();
						}
					}, {
						text : '重 置',
						handler : function() {
							queryForm.form.reset();
						}
					}, {
						text : '关 闭',
						handler : function() {
							queryWin.close();
							// queryObjectTestWin =
							// null;
							// queryWin = null;
						}
					}]
				});

				queryWin.show(this);
			}
		}]
	});

	toolbar.addClass('ExtToolBar');

	toolbar.render('listToolbarArea');

	ds = new Ext.data.JsonStore({
				idProperty : 'PAGINATION_NUMBER',
				remoteSort : false,
				totalProperty : 'totalCount',
				root : 'queryList',
				fields : fields,
				proxy : new Ext.data.HttpProxy({
							url : context + '/app/cellaction.do?method=list'
						})
			});

	/* 设置自定义参数 */
	ds.on('beforeload', function(thiz, options) {
				var params = getQueryTestParams();
				Ext.apply(thiz.baseParams, params);
			});

	storeOnLoad(0);
}

function getQueryTestParams() {
	return {
		start : 0,
		limit : pageSize,
		cname : $('cname_') ? $('cname_').value : '',
		cunit : $('cunit_') ? $('cunit_').value : '',
		ctype : $('ctype') ? $('ctype1').value : '',
		carea : $('carea') ? $('carea_').value : ''
	};
}

function storeOnLoad(start) {
	Ext.MessageBox.wait('加载数据过程中...');

	ds.load({
				params : {
					start : start,
					limit : pageSize
				},
				callback : function(r, options, success) {
					initInfoList(r, options, success);
				}
			});
}

function storeReload(start) {
	Ext.MessageBox.wait('加载数据过程中...');

	ds.reload({
				params : {
					start : start,
					limit : pageSize
				},
				callback : function(r, options, success) {
					initInfoList(r, options, success);
				}
			});
}

function initInfoList(r, options, success) {
	if (success == false) {
		Ext.Msg.alert('ERROR', '加载数据出现异常.');
	} else {
		var total = ds.getCount();// 数据行数

		while ($(querykey).rows.length > 1) {
			$(querykey).deleteRow($(querykey).rows.length - 1);
		}

		var arr = [];
		for (var j = 0; j < total; j++) {
			var record = ds.getAt(j);

			var tr = $(querykey).insertRow($(querykey).rows.length);

			var i = 0;
			Ext.each(chkColumModel, function(item) {
				var td = tr.insertCell(-1);
				td.className = item.clazz;
				if (showchk && i == 0) {
					td.innerHTML = '<div class="divn"><input type="checkbox" name="chkitem" value="'
							+ j
							+ '">&nbsp;'
							+ renderHeader(record.data[item.dataIndex], cm,
									record, j, i, ds) + '</div>';
				} else {
					if (item.dataIndex == 'CELLBDLIST') {
						var bdlist = record.data[item.dataIndex];
						if (bdlist.length > 0) {
							var str = '';
							Ext.each(bdlist, function(item) {
								str += '<div class="divn" style="width:150px;">&nbsp;'
										+ item.CB_NAME
										+ '&nbsp;<a href="javascript:void(0);" onClick="javascript:purview(\''
										+ item.CB_ID
										+ '\', \''
										+ record.data['C_NAME']
										+ ': '
										+ item.CB_NAME
										+ '\');" title="设置标段控制权限">权限设置</a></div>';
							});

							td.innerHTML = str;
						}
					} else {
						td.innerHTML = '<div class="divn">'
								+ renderHeader(record.data[item.dataIndex], cm,
										record, j, i, ds) + '</div>';
					}
				}

				i++;
			});
		}

		var ys = ds.getTotalCount() % pageSize;

		var sh = parseInt(ds.getTotalCount() / pageSize);

		var totalPage = (ys == 0 ? sh : sh + 1);

		var pageNO = parseInt(options.params.start / options.params.limit) + 1;

		var str1 = '共' + ds.getTotalCount() + '条数据';

		if (ds.getTotalCount() > 0) {
			str1 += ',第' + pageNO + '/' + totalPage + '页';
		}

		$('pageDisplay').innerHTML = str1;

		var str = '';

		if (pageNO > 1) {
			str += '<a href="javascript:storeReload(\'0\');">首页</a>';
		} else if (totalPage > 1) {
			str += '首页';
		}

		if (pageNO > 1) {
			var start = pageSize * (pageNO - 2)

			str += '&nbsp;<a href="javascript:storeReload(\'' + start
					+ '\');">上一页</a>';
		}

		if (pageNO < totalPage) {
			var start = pageSize * pageNO

			str += '&nbsp;<a href="javascript:storeReload(\'' + start
					+ '\');">下一页</a>';
		}

		if (pageNO < totalPage) {
			var start = pageSize * (totalPage - 1)

			str += '&nbsp;<a href="javascript:storeReload(\'' + start
					+ '\');">尾页</a>';
		} else if (pageNO == totalPage && pageNO > 1) {
			str += '尾页';
		}

		$('pageRoll').innerHTML = str;

		Ext.MessageBox.hide();
	}
}

function renderHeader(value, cellmeta, record, rowIndex, columnIndex, store) {
	var fieldName = cm.getDataIndex(columnIndex);
	if (fieldName == 'CAR_DATE') {
		return formatDate(value);
	}
	return value;
}

function purview(id, name) {
	var plist = purviewlist(id, name, 'cell', 'APP_NS_CELL', 'bdjlpurview',
			'' + name + '');

	var purviewWin = new Ext.Window({
		        title : '工程标段监理权限设置',
				renderTo : Ext.getBody(),
				layout : 'fit',
				width : 600,
				height : 400,
				resizable : true,
				plain : true,
				modal : true,
				closable : true,

				items : [plist]
			});

	purviewWin.show(this);
}